package com.dy.nygl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.ApiError;
import com.dy.nygl.pojo.Energy;
import com.dy.nygl.pojo.EquipMonit;
import com.dy.nygl.service.EnergyService;

import jdk.nashorn.internal.ir.RuntimeNode.Request;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/energy")
public class EnergyUsageController {

	private String select = "jsp/a.jsp";

	private List<Energy> energyList;

	@Resource(name = "energyService")
	private EnergyService energyService;

	public List<Energy> getEnergyList() {
		return energyList;
	}

	public void setEnergyList(List<Energy> energyList) {
		this.energyList = energyList;
	}

	public String getSelect() {
		return select;
	}

	public void setSelect(String select) {
		this.select = select;
	}

	@RequestMapping("/getSelect.action")
	public void getSelect(@RequestParam("select") String select, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		setSelect(select);
		out.print(2);
	}

	@RequestMapping("/updateThis.action")
	public ModelAndView updateThis(@RequestParam("id") Integer id, HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		model.addObject("id", id);
		Energy energy = energyService.selectById(id);
		model.addObject("name", energy.getName());
		model.addObject("actual", energy.getActual());
		model.addObject("predict", energy.getPredict());
		model.addObject("unit", energy.getUnit());
		model.setViewName("energy_update");
		return model;
	}

	@RequestMapping("/getList.action")
	public ModelAndView getList(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		ModelAndView model = new ModelAndView();

		energyList = getEnergyList();
		model.addObject("energyList", energyList);
		model.setViewName("energy_usage");
		return model;
	}

	@RequestMapping("/showList.action")
	public ModelAndView showList(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		ModelAndView model = new ModelAndView();
		energyList = energyService.selectAll();
		model.addObject("energyList", energyList);
		model.setViewName("details_energy");
		return model;
	}

	@RequestMapping("/showList1.action")
	public ModelAndView showList1(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		ModelAndView model = new ModelAndView();
		energyList = energyService.selectAll();
		select = getSelect();
		model.addObject("option", select);
		model.addObject("energyList", energyList);
		model.setViewName("database");
		return model;
	}

	@RequestMapping("/update.action")
	public void update() {
		Energy energy = new Energy();
		energy.setId(1);
		energy.setActual("800");
		energyService.update(energy);

	}

	@RequestMapping("/setList.action")
	public void setList(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		energyList = energyService.selectAll();
		setEnergyList(energyList);
		PrintWriter out = response.getWriter();
		out.print(8);
	}

	@RequestMapping("/api.getall.action")
	public ModelAndView getAll(@RequestParam("token") String token, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		ModelAndView model = new ModelAndView();
		energyList = energyService.selectAll();
		token = request.getParameter("token");
		if (token.equals("18758171751")) {
			Object json = JSON.toJSON(energyList).toString();
			model.addObject("json", json);
			model.setViewName("api_getall");
			return model;
		}
		ApiError apiError = new ApiError();
		apiError.setResultcode("101");
		apiError.setReason("错误的token!请输入聪聪工作室正确的 Token令牌！");
		apiError.setResult("null");
		apiError.setError_code("10001");
		Object jsonerr = JSON.toJSON(apiError).toString();
		model.addObject("jsonerr", jsonerr);
		model.setViewName("json_err");
		return model;

	}

	@RequestMapping(value = "/updateSelective.action", method = { RequestMethod.POST })
	public ModelAndView updateSelective(HttpServletRequest request, HttpSession session) throws IOException {

		String id1 = request.getParameter("id");
		int id = Integer.parseInt(id1.trim());
		String name = request.getParameter("name");
		String actual = request.getParameter("actual");
		String predict = request.getParameter("predict");
		String unit = request.getParameter("unit");

		Energy energy = new Energy();
		energy.setId(id);
		energy.setName(name.trim());
		energy.setActual(actual.trim());
		energy.setPredict(predict.trim());
		energy.setUnit(unit.trim());
		energyService.update(energy);

		ModelAndView model = new ModelAndView();

		energyList = getEnergyList();
		model.addObject("energyList", energyList);
		model.setViewName("database");
		return model;
	}

}
